package com.holly.unit.bpmn.modular.activiti.controller;

import com.holly.unit.bpmn.activiti.ActivitiOperator;
import com.holly.unit.bpmn.activiti.pojo.*;
import com.holly.unit.bpmn.modular.activiti.adapter.SysUserToComboModel;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import com.holly.unit.system.api.UserServiceApi;
import com.holly.unit.system.api.pojo.user.SysUserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述: 流程实例管理
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/3/7 14:01
 */
@RestController
@ApiResource(name = "流程实例相关API")
@Api(tags = "流程实例相关API")
@Slf4j
public class ActProcessInsController {

    @Resource
    private UserServiceApi userServiceApi;

    @Resource
    private ActivitiOperator operator;

    @ApiOperation("运行中的流程实例列表")
    @GetResource(name = "运行中的流程实例列表", path = "/processIns/getRunningProcess")
    public ResponseData getRunningProcess(RunProcessInsRequest request) {
        // 获取所有剔除删除后的用户
        List<SysUserDTO> users = userServiceApi.queryAllUserList(null);
        List<ComboModel> allUser = SysUserToComboModel.convert(users);
        List<RunProcessInsResponse> runningProcess = operator.getRunningProcess(request.getName(), request.getCategoryId(), request.getKey(), allUser);
        return new SuccessResponseData(runningProcess);
    }

    @ApiOperation("通过id删除运行中的实例")
    @PostResource(name = "通过id删除运行中的实例", path = "/processIns/delInsByIds")
    public ResponseData delInsByIds( @RequestBody @Validated(ProcessInsDelByIdsRequest.delInsByIds.class) ProcessInsDelByIdsRequest request) {
        String ids = request.getIds();
        String reason = request.getReason();
        operator.delInsByIds(ids,reason);
        return new SuccessResponseData();
    }

    @ApiOperation("激活或挂起流程实例")
    @PostResource(name = "激活或挂起流程实例", path = "/processIns/updateInsStatus")
    public ResponseData updateStatus( @RequestBody @Validated(ProcessInsUpdateStatusRequest.updateStatus.class) ProcessInsUpdateStatusRequest request) {
        operator.updateStatus(request.getId(),request.getStatus());
        return new SuccessResponseData();
    }

    @ApiOperation("获取结束的的流程实例")
    @GetResource(name = "获取结束的的流程实例", path = "/processIns/getFinishedProcess")
    public ResponseData getFinishedProcess(FinishProcessInsRequest request) {
        // 获取所有剔除删除后的用户
        List<SysUserDTO> users = userServiceApi.queryAllUserList(null);
        List<ComboModel> allUser = SysUserToComboModel.convert(users);
        List<HistoricProcessInsResponse> finishedProcess = operator.getFinishedProcess(request.getName(), request.getCategoryId(), request.getKey(), request.getStartDate(), request.getEndDate(), allUser);
        return new SuccessResponseData(finishedProcess);
    }

    @ApiOperation("通过id删除已结束的实例")
    @PostResource(name = "通过id删除已结束的实例", path = "/processIns/delHistoricInsByIds")
    public ResponseData delHistoricInsByIds(@RequestBody @Validated(ProcessHistoricInsByIdRequest.delHistoricInsByIds.class)ProcessHistoricInsByIdRequest request) {
        operator.delHistoricInsByIds(request.getIds());
        return new SuccessResponseData();
    }
}
